#!/usr/bin/env python3
from api import ApiService

class Note(object):
    def __init__(self):
        pass

    title = 'test'
    privacy = 'public'
    can_reply = 'true'
    content = 'test'
    pids = None
    layout_pid = ''
    desc_pid = ''
    image_pid = ''
    images = None

    def __str__(self):
        return self.pids

class NoteService(object):

    API_HOST = 'https://api.douban.com'
    BASE_API = API_HOST + '/v2/note/%s'

    ADD_API = API_HOST + '/v2/notes'
    #noteid
    COMMENT_API = API_HOST + '/v2/note/%s/comments'
    #noteid
    LIKE_API = API_HOST + '/v2/note/%s/like'
    #userId
    NOTES_API = API_HOST + '/v2/note/user_created/%s'
    #userid
    LIKES_API = API_HOST + '/v2/note/user_liked/%s'


    def __init__(self, access_token):
        self.apiService = ApiService(access_token)

    def addNote(self, note):
        if not note.pids:
            data = {'title':note.title, 'privacy': note.privacy, 'can_reply':note.can_reply,
                'content':note.content}
            return self.apiService.post(self.ADD_API, data)
        # else:
        #     pids_str = ''
        #     for pid in pids:
        #         pids_str += 'p_' + pid + ','

        #     fields = [('title', note.title),('privacy', note.privacy),('can_reply', note.can_reply),
        #     ('content',note.content), ('pids', note.pids)]
        #     files = [('image_1', 'note.jpg', open('D:\\OscGit\\python\\res\\note0.jpg','rb'))]
        #     return self.apiService.multi_post(self.ADD_API, fields, files)


    def delNote(self, noteId):
        return self.apiService.delete(self.BASE_API.replace("%s", noteId))

    def updateNote(self, noteId, note):
        data = {'title':note.title, 'privacy': note.privacy, 'can_reply':note.can_reply, 
            'content':note.content, 'pids': note.pids, 'layout_pid': note.layout_pid, 
            'desc_pid': note.desc_pid, 'image_pid': note.image_pid}
        return self.apiService.put(self.BASE_API.replace("%s", noteId), data)

    def getNote(self, noteId):
        return self.apiService.get(self.BASE_API.replace("%s", noteId))

    def getNotes(self, userId):
        return self.apiService.get(self.NOTES_API.replace("%s", userId))

    def getNoteComments(self, noteId):
        return self.apiService.get(self.COMMENT_API.replace("%s", noteId))

    def getLikeNotes(self, userId):
        return self.apiService.get(self.LIKES_API.replace("%s", userId))

    def likeNote(self, noteId):
        return self.apiService.post(self.LIKE_API.replace("%s", noteId))

    def unlikeNote(self, noteId):
        return self.apiService.delete(self.LIKE_API.replace("%s", noteId))

    def uploadImg(self, noteId):
        return self.apiService.multi_post(self.BASE_API.replace("%s", noteId), 
            [('pids', 'p_1,p_2'),('layout_1','R'),('layout_2','C'),('content','this is code for you<图片p_1>hi!!my dear.<图片p_2>')],
            [('image_1', 'note.jpg', open('D:\\OscGit\\python\\res\\note0.jpg','rb')),
            ('image_2', 'note0.jpg', open('D:\\OscGit\\python\\res\\note0.jpg','rb'))])

#test
noteService = NoteService('d096fa52dc330f6871c4eef894992e38')
#print(noteService.getNotes('61725334'))
print(noteService.getNote('280775139'))
#print(noteService.delNote('281177949'))
note = Note()
note.title = 'add_test_api'
note.content = 'i love you forever!'
#print(noteService.addNote(note))
note.title = 'update_test_api'
#print(noteService.updateNote('281177949',note))
#print(noteService.getNoteComments('281177949'))
#print(noteService.getLikeNotes('61725334'))
#print(noteService.likeNote('281177949'))
#print(noteService.unlikeNote('281177949'))
#print(noteService.uploadImg('280775139'))